package test;

import java.awt.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class RegisterFrame extends JFrame {

    private JPanel panel;
    private JTextField usernameField;
    private JPasswordField pwdField;
    private JPasswordField pwdField2;
    private JButton okBtn;
    private JLabel msgLbl2;
    private JLabel msgLbl;

    /**
     * Create the frame.
     */
    public RegisterFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 540, 400);
        panel = new JPanel();
        panel.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(panel);
        panel.setLayout(null);

        JLabel lblNewLabel = new JLabel("欢迎注册");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 36));
        lblNewLabel.setBounds(104, 24, 281, 66);
        panel.add(lblNewLabel);

        JLabel usernameLbl = new JLabel("");
        usernameLbl.setBounds(62, 142, 58, 15);
        panel.add(usernameLbl);

        JLabel pwdLbl = new JLabel("");
        pwdLbl.setBounds(62, 170, 58, 15);
        panel.add(pwdLbl);

        JLabel pwdLbl2 = new JLabel("");
        pwdLbl2.setBounds(62, 198, 58, 15);
        panel.add(pwdLbl2);


        msgLbl = new JLabel("");
        msgLbl.setFont(new Font("宋体", Font.BOLD, 20));
        msgLbl.setForeground(Color.RED);
        msgLbl.setBounds(262, 112, 140, 28);
        panel.add(msgLbl);

        msgLbl2 = new JLabel("");
        msgLbl2.setFont(new Font("宋体", Font.BOLD, 20));
        msgLbl2.setForeground(Color.RED);
        msgLbl2.setBounds(262, 198, 180, 28);
        panel.add(msgLbl2);

        usernameField = new JTextField();
        usernameField.setBounds(162, 109, 86, 31);
        panel.add(usernameField);
        usernameField.setColumns(10);

        pwdField = new JPasswordField();
        pwdField.setBounds(162, 152, 86, 31);
        panel.add(pwdField);

        pwdField2 = new JPasswordField();
        pwdField2.setBounds(162, 195, 86, 31);
        panel.add(pwdField2);

        okBtn = new JButton("确定");
        okBtn.setBounds(162, 295, 97, 23);
        okBtn.setContentAreaFilled(false);//设置透明
        okBtn.setFont(new java.awt.Font("华文彩云", 1, 25));//设置字体
        okBtn.setBorderPainted(false);//设置无边框
        panel.add(okBtn);

        usernameField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                if (usernameField.getText().equals("")) {
                    msgLbl.setText("昵称不能为空");
                }
            }

            @Override
            public void focusGained(FocusEvent e) {
                msgLbl.setText("");
            }
        });


        pwdField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                if (!pwdField2.getText().equals("")) {
                    if (pwdField.getText().equals(pwdField2.getText())) {
                        //匹配
                        msgLbl2.setText("匹配成功");
                    } else {
                        //不匹配
                        msgLbl2.setText("两次密码不匹配");
                    }
                } else {
                    msgLbl2.setText("");
                }
            }
        });

        pwdField2.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                if (!pwdField2.getText().equals("")) {
                    if (pwdField.getText().equals(pwdField2.getText())) {
                        //匹配
                        msgLbl2.setText("匹配成功");
                    } else {
                        //不匹配
                        msgLbl2.setText("两次密码不匹配");
                    }
                } else {
                    msgLbl2.setText("");
                }
            }
        });


        JFrame pFrame = this;
        okBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //确定按钮触发
                if (usernameField.getText().equals("")) {
                    msgLbl.setText("请输入昵称");
                    return;
                }

                if (pwdField.getText().equals("")) {
                    msgLbl2.setText("密码不能为空");
                } else if (pwdField2.getText().equals("")) {
                    msgLbl2.setText("请再次输入确认密码");
                } else if (!pwdField.getText().equals(pwdField2.getText())) {
                    msgLbl2.setText("两次密码不匹配");
                } else {
                    //第一个界面成功
                    pFrame.dispose();
                    LoginFrame loginFrame = new LoginFrame(usernameField.getText(), pwdField.getText());
                    loginFrame.getFrame().setVisible(true);
                }
            }
        });

        ImageIcon icon = new ImageIcon("test/image/05.jpg");
        JLabel lblBackground = new JLabel(); // 创建一个标签组件对象
        lblBackground.setIcon(icon); // 设置标签组件要显示的图标
        lblBackground.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight()); // 设置组件的显示位置及大小
        this.getContentPane().add(lblBackground);
    }


}
